package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @Author by 孙有泽
 * @Date 2025/3/23 13:01
 * @Description 用户地址簿管理
 */
@RestController
@RequestMapping("/user/addressBook")
@Api("用户地址簿管理")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressService addressService;

    /*
     * @author 孙有泽
     * @date 2025/3/23 13:04
     * @description 查询当前登录用户的所有地址信息
     */
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> list(){

        return Result.success(addressService.list());
    }
    /*
     * @author 孙有泽
     * @date 2025/3/23 13:13
     * @description 新增地址
     */

    @PostMapping
    @ApiOperation("新增地址")
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址,参数:{}",addressBook);
        addressService.save(addressBook);
        return Result.success();
    }

    /*
     * @author 孙有泽
     * @date 2025/3/23 13:36
     * @description 根据id查询地址
     */

    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        return Result.success(addressService.getById(id));
    }
    /*
     * @author 孙有泽
     * @date 2025/3/23 13:41
     * @description 修改地址
     */
    @ApiOperation("修改地址")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressService.update(addressBook);
        return Result.success();
    }
    /*
     * @author 孙有泽
     * @date 2025/3/23 13:48
     * @description 设置默认地址
     */

    @ApiOperation("value = \"设置默认地址\"")
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
    addressService.setDefault(addressBook);
       return Result.success();
    }

    /*
     * @author 孙有泽
     * @date 2025/3/23 13:54
     * @description 根据id查询默认地址
     */
    @ApiOperation("根据id查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        return Result.success(addressService.getDefault());
    }
    /*
     * @author 孙有泽
     * @date 2025/3/23 13:57
     * @description T0DO根据id删除购物车
     */

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id) {
        addressService.deleteById(id);
        return Result.success();
    }




    }
